<?php


namespace app\api\controller;
use app\api\model\Base as bs;
use app\api\model\RedisModel;
use think\Db;
use think\Request;
use lib\PHPMailer;
//测试类
class Test extends Base
{
    //高并发地址http://127.0.0.1/lession_admin/public/index.php/api/test
    //ab -n1000 -c50  http://127.0.0.1/lession_admin/public/index.php/api/test
    //-n一共多少次请求，-c瞬间的并发数
    public function test(){
        if(Request::instance()->method()== 'GET'){
            $courseId = 1;
            $id = I('id');
            $id = (int)$id;
            $redis = my_redis();
            return $redis->smembers('course_' . $courseId)[1];
            if($redis->smembers('course_' . $courseId)[1] <= count($redis->smembers('user_' . $courseId))){
                $this->apiReturn(303,'课程已被抢完',["num"=>count($redis->smembers('user_1'))]);
            }
            $result = $redis->sismember('user_' . $courseId, $id);
            if ($result) {
                $this->apiReturn(304,'您不能重复抢课');
            }
            $result_Add = $redis->sadd('user_' . $courseId, $id);
            if ($result_Add) {
                $this->apiReturn(200,'抢课成功');
            }
            $this->apiReturn(305,'抢课失败');
        }
    }
    public function test2(){
//        $redis = my_redis();
//        var_dump($redis);
////        $redis->sadd('course_1',"课程1", 100);
//       $redis->sadd('user_1',3);
//       $redis->sadd('user_1',2);
//       $redis->sadd('user_1',5);
//        dump($redis->smembers('user_1'));

        $servers = [
            'tcp://127.0.0.1:6379',
            'tcp://127.0.0.1:6381',
            'tcp://127.0.0.1:6382',
            'tcp://127.0.0.1:6383',
            'tcp://127.0.0.1:6384',
            'tcp://127.0.0.1:6385'
        ];
        $options = ['cluster' => 'redis'];
        $client = new \Predis\Client($servers, $options);
        $client->set('name1', '1111111');
        $client->set('name2', '2222222');
        $client->set('name3', '3333333');
        $name1 = $client->get('name1');
        $name2 = $client->get('name2');
        $name3 = $client->get('name3');
        var_dump($name1, $name2, $name3);die;
    }
}